#include "algorithm.h"
#include <stdio.h>

double AmpCCT = 28000;
double FitCOE = 0.04;
double OffsetCCT = 1700;
double AFitCoE = 5;
double lamda_0 = 1400;
double MainCOE = 0.06752;
double SubCOE1 = 1.6E-6;
double SubCOE2 = 0.0214;

double LighitSensor_GetLux(sLightSensorData_t *pSensorData)
{
	uint32_t dwAdc_R = pSensorData->R_Value;
	uint32_t dwAdc_G = pSensorData->G_Value;
	uint32_t dwAdc_B = pSensorData->B_Value;
	uint32_t dwAdc_IR = pSensorData->IR_Value;
	
	double CCT = AmpCCT * ((FitCOE * (2 * dwAdc_B) / (dwAdc_R + dwAdc_IR)) / (1 + abs(FitCOE  * (2 * dwAdc_B) / (dwAdc_R + dwAdc_IR)))) + OffsetCCT;
	double lamdaPeak = 2898000 / CCT;
	double AF_IR = (1/2)*(AFitCoE * (lamda_0 - lamdaPeak)/(1 + abs(AFitCoE * (lamda_0 - lamdaPeak)) + 1));
	double Lux = MainCOE * ((dwAdc_G + dwAdc_B) / 2) + (AF_IR * ((SubCOE1 * dwAdc_IR * dwAdc_IR) + SubCOE2 * dwAdc_IR));

	printf("\r\nCCT=%lf,Lux=%lf\r\n", CCT, Lux);
	
	return Lux;
}


